<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <th:block th:include="include :: header('收款认领列表')"/>
    <th:block th:include="include :: select2-css" />
    <th:block th:include="include :: bootstrap-select-css" />
</head>
<body class="gray-bg">
<div class="container-div">
    <div class="row">
        <div class="col-sm-12 search-collapse">
            <form id="formId">
                <div class="select-list">
                    <ul>
                        <li>
                            <label>是否审核：</label>
                            <select name="checkStatus" th:with="type=${@dict.getType('work_commission')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>是否认领：</label>
                            <select name="isBelong" th:with="type=${@dict.getType('work_cancel_belong')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>来款人：</label>
                            <input type="text" name="incomingName"/>
                        </li>
                        <li>
                            <label>来款银行：</label>

                            <select class="form-control" name="bankName" th:with="type=${@dict.getType('work_bank')}">
                                <option value="">所有</option>
                                <option th:each="dict : ${type}" th:text="${dict.dictLabel}"
                                        th:value="${dict.dictValue}"></option>
                            </select>
                        </li>
                        <li>
                            <label>来款账号：</label>
                            <input type="text" name="bankAccount"/>
                        </li>
                        <li>
                            <label>来款额度：</label>
                            <input type="text" name="money"/>
                        </li>

                        <li>
                            <label>来款日期：</label>
                            <input type="text" class="time-input" placeholder="请选择来款日期" name="incomingDate"/>
                        </li>
                        <li>
                            <label>认领人：</label>
                            <input type="text" name="belong"/>
                        </li>
                        <li>
                            <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i
                                    class="fa fa-search"></i>&nbsp;搜索</a>
                            <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i
                                    class="fa fa-refresh"></i>&nbsp;重置</a>
                        </li>
                    </ul>
                </div>
            </form>
        </div>

        <div class="btn-group-sm" id="toolbar" role="group">
            <a class="btn btn-success" onclick="$.operate.add()" shiro:hasPermission="work:incoming:add" name="bbb">
                <i class="fa fa-plus"></i> 来款登记
            </a>
            <a class="btn btn-primary single disabled" onclick="$.operate.edit()" shiro:hasPermission="work:incoming:edit" name="bbb">
                <i class="fa fa-edit"></i> 编辑
            </a>
            <a class="btn btn-primary single disabled" onclick="belong(0)" shiro:hasPermission="work:incoming:belong" name="bbb">
                <i class="fa fa-edit"></i> 来款认领
            </a>
            <a class="btn btn-primary single disabled" onclick="belong(1)" shiro:hasPermission="work:incoming:belong" name="bbb">
                <i class="fa fa-edit"></i> 注销认领
            </a>
            <!--                <a class="btn btn-danger multiple disabled" onclick="$.operate.removeAll()" shiro:hasPermission="work:incoming:remove">
                                <i class="fa fa-remove"></i> 删除
                            </a>-->
            <a class="btn btn-primary single disabled" onclick="look()" shiro:hasPermission="work:incoming:look" >
                <i class="fa fa-edit"></i> 查看销账记录
            </a>
            <a class="btn btn-warning" onclick="$.table.exportExcel()" shiro:hasPermission="work:incoming:export">
                <i class="fa fa-download"></i> 导出
            </a>

            <a class="btn btn-primary" onclick="cancel(1)" shiro:hasPermission="work:incoming:cancel" name="ccc">
                <i class="fa fa-money"></i> 批量销账(无发票)
            </a>
            <a class="btn btn-primary" onclick="cancel(2)" shiro:hasPermission="work:incoming:cancel"  name="ccc">
                <i class="fa fa-money"></i> 批量销账(有发票)
            </a>
            <a class="btn btn-primary" onclick="check()" shiro:hasPermission="work:incoming:check"  name="bbb">
                <i class="fa fa-gavel"></i> 审核
            </a>
            <a class="btn btn-primary" onclick="grant()" shiro:hasPermission="work:incoming:grant"  name="bbb">
                <i class="fa fa-money"></i> 提成
            </a>
        </div>
        <div class="col-sm-12 select-table table-striped">
            <table id="bootstrap-table"></table>
        </div>
    </div>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: select2-js" />
<th:block th:include="include :: bootstrap-select-js" />
<script th:inline="javascript">
    var addFlag = [[${@permission.hasPermi('work:incoming:add')}]];
    var editFlag = [[${@permission.hasPermi('work:incoming:edit')}]];
    var removeFlag = [[${@permission.hasPermi('work:incoming:remove')}]];
    var cancelFlag = [[${@permission.hasPermi('work:incoming:cancel')}]];
    var belongFlag = [[${@permission.hasPermi('work:incoming:belong')}]];


    var prefix = ctx + "work/incoming";
    var bankNoDatas = [[${@dict.getType('work_bank')}]];
    var commission = [[${@dict.getType('work_commission')}]];
    var canceltype = [[${@dict.getType('work_cancel_type')}]];

    var path = window.location.pathname.split("/")[2].toLowerCase()

    var url = path == 'incoming' ? 'list' : 'reglist';

    $(function () {
        if(path=='reg' ){
            $('[name="ccc"]').hide();
        }else if(path=='incoming' ){
            $('[name="bbb"]').hide();
        }
        var options = {
            url: prefix + "/" + url,
            createUrl: prefix + "/add",
            updateUrl: prefix + "/edit/{id}",
            removeUrl: prefix + "/remove",
            exportUrl: prefix + "/export",
            uniqueId: "incomingId",
            columns: [{
                checkbox: true
            },
                {
                    field: 'incomingId',
                    title: '认领ID'

                },

                {
                    field: 'incomingName',
                    title: '来款人'
                },
                {
                    field: 'bankName',
                    title: '来款银行',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(bankNoDatas, value);
                    }
                },
                {
                    field: 'bankAccount',
                    title: '来款账号'
                },
                {
                    field: 'money',
                    title: '来款额度'
                },
                {
                    field: 'refund',
                    title: '返款金额',
                    visible: !cancelFlag
                },
                {
                    field: 'taxes',
                    title: '税金'
                },
                {
                    field: 'expenseLimit',
                    title: '已销额度'
                },
                {
                    field: '',
                    title: '可销额度',
                    formatter: function (value, row, index) {
                        return row.money - (row.refund + row.taxes + row.expenseLimit);//可销额度=来款-（返款+税金+已销）
                    }
                },
                {
                    field: 'incomingDate',
                    title: '来款日期'
                },

                {
                    field: 'belong',
                    title: '认领人'
                },
                {
                    field: 'remark',
                    title: '备注'
                },
                {
                    field: 'cancelType',
                    title: '销账类型',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(canceltype, value);
                    }
                },
                {
                    field: 'checkStatus',
                    title: '审核状态',
                    formatter: function (value, row, index) {
                        return $.table.selectDictLabel(commission, value);
                    }
                }]
        };
        $.table.init(options);
    });

    function look() {
        var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (id.length != 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        var cancel_type = $.table.selectColumns('cancelType');

        if (cancel_type == 0) {
            $.modal.alertWarning("该记录还未销账");
            return;
        }
        var options = {
            title: cancel_type == 1 ? "销账管理(无发票)" : "销账管理(有发票)",
            url: (prefix + "/" + (cancel_type == 1 ? "off" : "cancel") + "/{id}").replace("{id}", id),
            width: 1200,
            btn: false
        };
        $.modal.openOptions(options);
    }

    function cancel(status) {
        var belong = $.table.selectColumns('belong');
        if (belong == '' || belong == undefined) {

            $.modal.alertWarning("请先认领");
            return;

        }
        var cancel_type = $.table.selectColumns('cancelType');
        if (cancel_type > 0 && cancel_type != status) {
            $.modal.alertWarning("已销账，请选择相对应的销账类型：" + (cancel_type == 1 ? "销账管理(无发票)" : "销账管理(有发票)"));
            return;
        }
        let str = status == 1 ? "销账管理(无发票)" : "销账管理(有发票)";
        var id = $.common.isEmpty(table.options.uniqueId) ? $.table.selectFirstColumns() : $.table.selectColumns(table.options.uniqueId);
        if (id.length != 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }

        var url = (prefix + "/" + (status == 1 ? "off" : "cancel") + "/{id}").replace("{id}", id);
        $.modal.open(str, url);
    }

    function belong(status) {

        var ids = $.table.selectColumns("incomingId");
        if (ids.length != 1) {
            $.modal.alertWarning("请选择一条记录");
            return;
        }
        var str = status == 0 ? "认领" : "注销"
        $.modal.confirm("确认要" + str + "选中的" + ids.length + "条数据吗?", function () {
            $.post(prefix + "/belong/" + ids, {status: status}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh();
            });
        });
    }

    function check() {
        var ids = $.table.selectColumns('incomingId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录审核");
            return;
        }
        $.modal.confirm("确认要审核选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/check", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("审核成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });
    }

    function grant() {
        var ids = $.table.selectColumns('incomingId');

        if (ids.length == 0) {
            $.modal.alertWarning("请至少选择一条记录提成");
            return;
        }
        $.modal.confirm("确认要提成选中的" + ids.length + "条数据吗?", function () {

            $.post(prefix + "/grant", {ids: ids.join()}, function (result) {

                if (result.code == web_status.SUCCESS) {
                    $.modal.msgSuccess("操作成功！")
                } else {
                    $.modal.msgError(result.msg);
                }
                $.table.refresh()
            });
        });
    }
</script>
</body>
</html>